<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.12"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>emp-toolkit: /Users/wangxiao/git/emp-toolkit/emp-tool/utils/hash.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo-full-doxygen.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">emp-toolkit
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.12 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('hash_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">hash.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="hash_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#ifndef HASH_H__</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#define HASH_H__</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="utils_2block_8h.html">block.h</a>&quot;</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="config_8h.html">config.h</a>&quot;</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="preprocessor">#include &lt;openssl/sha.h&gt;</span></div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="utils__ec_8h.html">utils_ec.h</a>&quot;</span></div><div class="line"><a name="l00012"></a><span class="lineno"><a class="line" href="class_hash.html">   12</a></span>&#160;<span class="keyword">class </span><a class="code" href="class_hash.html">Hash</a> {</div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;    SHA_CTX hash;</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;    <span class="keywordtype">char</span> buffer[<a class="code" href="config_8h.html#a248d7b4360cb56aceb6b29be5990ef75">HASH_BUFFER_SIZE</a>];</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;    <span class="keywordtype">int</span> size = 0;</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;    <span class="keyword">public</span>:</div><div class="line"><a name="l00017"></a><span class="lineno"><a class="line" href="class_hash.html#afd43dfd1a98f58dd6ea17eb3bba8a6c2">   17</a></span>&#160;    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="class_hash.html#afd43dfd1a98f58dd6ea17eb3bba8a6c2">DIGEST_SIZE</a> = 20;</div><div class="line"><a name="l00018"></a><span class="lineno"><a class="line" href="class_hash.html#af482880ad75b67a09d2dcb5e86244d80">   18</a></span>&#160;    <a class="code" href="class_hash.html#af482880ad75b67a09d2dcb5e86244d80">Hash</a>() {</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;        SHA1_Init(&amp;hash);</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;    }</div><div class="line"><a name="l00021"></a><span class="lineno"><a class="line" href="class_hash.html#a4e4b4797dda8678aaed058bae155813e">   21</a></span>&#160;    <a class="code" href="class_hash.html#a4e4b4797dda8678aaed058bae155813e">~Hash</a>() {</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;    }</div><div class="line"><a name="l00023"></a><span class="lineno"><a class="line" href="class_hash.html#a4bb9da7bf411c5db9af391fc3ec74f81">   23</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_hash.html#a4bb9da7bf411c5db9af391fc3ec74f81">put</a>(<span class="keyword">const</span> <span class="keywordtype">void</span> * data, <span class="keywordtype">int</span> nbyte) {</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;        <span class="keywordflow">if</span> (nbyte &gt; <a class="code" href="config_8h.html#a248d7b4360cb56aceb6b29be5990ef75">HASH_BUFFER_SIZE</a>)</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;            SHA1_Update(&amp;hash, data, nbyte);</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;        <span class="keywordflow">else</span> <span class="keywordflow">if</span>(size + nbyte &lt; <a class="code" href="config_8h.html#a248d7b4360cb56aceb6b29be5990ef75">HASH_BUFFER_SIZE</a>) {</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;            memcpy(buffer+size, data, nbyte);</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;            size+=nbyte;</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;        } <span class="keywordflow">else</span> {</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;            SHA1_Update(&amp;hash, (<span class="keywordtype">char</span>*)buffer, size);</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;            memcpy(buffer, data, nbyte);</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;            size = nbyte;</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;        }</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;    }</div><div class="line"><a name="l00035"></a><span class="lineno"><a class="line" href="class_hash.html#a86ad3dcd18b0b279d4039e4404a1043c">   35</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_hash.html#a86ad3dcd18b0b279d4039e4404a1043c">put_block</a>(<span class="keyword">const</span> <a class="code" href="garble_2block_8h.html#a65abbf31b0a30290189cdb5059b5f785">block</a>* <a class="code" href="garble_2block_8h.html#a65abbf31b0a30290189cdb5059b5f785">block</a>, <span class="keywordtype">int</span> nblock=1){</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;        <a class="code" href="class_hash.html#a4bb9da7bf411c5db9af391fc3ec74f81">put</a>(block, <span class="keyword">sizeof</span>(block)*nblock);</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;    }</div><div class="line"><a name="l00038"></a><span class="lineno"><a class="line" href="class_hash.html#a574b6b0ef1b80eb4b15809cc15257a3f">   38</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_hash.html#a574b6b0ef1b80eb4b15809cc15257a3f">digest</a>(<span class="keywordtype">char</span> * a) {</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;        <span class="keywordflow">if</span>(size &gt; 0) {</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;            SHA1_Update(&amp;hash, (<span class="keywordtype">char</span>*)buffer, size);</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;            size=0;</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;        }</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;        SHA1_Final((<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *)a, &amp;hash);</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;    }</div><div class="line"><a name="l00045"></a><span class="lineno"><a class="line" href="class_hash.html#a9c8d5864406c53c5ea83aa7bfb94c47d">   45</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_hash.html#a9c8d5864406c53c5ea83aa7bfb94c47d">reset</a>() {</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;        SHA1_Init(&amp;hash);</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;        size=0;</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;    }</div><div class="line"><a name="l00049"></a><span class="lineno"><a class="line" href="class_hash.html#a315c7f00ae3d92173fa452006b98501f">   49</a></span>&#160;    <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="class_hash.html#a315c7f00ae3d92173fa452006b98501f">hash_once</a>(<span class="keywordtype">void</span> * <a class="code" href="class_hash.html#a574b6b0ef1b80eb4b15809cc15257a3f">digest</a>, <span class="keyword">const</span> <span class="keywordtype">void</span> * data, <span class="keywordtype">int</span> nbyte) {</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;        (void )SHA1((<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *)data, nbyte, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *)digest);</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;    }</div><div class="line"><a name="l00052"></a><span class="lineno"><a class="line" href="class_hash.html#a0b3eafe3161e79b29a1501890bc3bbc0">   52</a></span>&#160;    <span class="keyword">static</span> <a class="code" href="garble_2block_8h.html#a65abbf31b0a30290189cdb5059b5f785">block</a> <a class="code" href="class_hash.html#a0b3eafe3161e79b29a1501890bc3bbc0">hash_for_block</a>(<span class="keyword">const</span> <span class="keywordtype">void</span> * data, <span class="keywordtype">int</span> nbyte) {</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;        <span class="keywordtype">char</span> <a class="code" href="class_hash.html#a574b6b0ef1b80eb4b15809cc15257a3f">digest</a>[20];</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;        <a class="code" href="class_hash.html#a315c7f00ae3d92173fa452006b98501f">hash_once</a>(digest, data, nbyte);</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;        <span class="keywordflow">return</span> _mm_load_si128((__m128i*)&amp;digest[0]);</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;    }</div><div class="line"><a name="l00057"></a><span class="lineno"><a class="line" href="class_hash.html#aa71d7a2c968e2594ef5c28d819617e30">   57</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_hash.html#aa71d7a2c968e2594ef5c28d819617e30">put_eb</a>(<span class="keyword">const</span> eb_t * eb, <span class="keywordtype">int</span> length) {</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;        uint8_t buffer[100];<span class="comment">//large enough to hold one.</span></div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;        <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0; i &lt; length; ++i) {</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;            <span class="keywordtype">int</span> eb_size = eb_size_bin(eb[i], <span class="keyword">false</span>);</div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;            eb_write_bin(buffer, eb_size, eb[i], <span class="keyword">false</span>);</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;            <a class="code" href="class_hash.html#a4bb9da7bf411c5db9af391fc3ec74f81">put</a>(buffer, eb_size);</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;        }</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;    }</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;};</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;<span class="preprocessor">#endif// HASH_H__</span></div><div class="ttc" id="class_hash_html_aa71d7a2c968e2594ef5c28d819617e30"><div class="ttname"><a href="class_hash.html#aa71d7a2c968e2594ef5c28d819617e30">Hash::put_eb</a></div><div class="ttdeci">void put_eb(const eb_t *eb, int length)</div><div class="ttdef"><b>Definition:</b> hash.h:57</div></div>
<div class="ttc" id="class_hash_html_a9c8d5864406c53c5ea83aa7bfb94c47d"><div class="ttname"><a href="class_hash.html#a9c8d5864406c53c5ea83aa7bfb94c47d">Hash::reset</a></div><div class="ttdeci">void reset()</div><div class="ttdef"><b>Definition:</b> hash.h:45</div></div>
<div class="ttc" id="garble_2block_8h_html_a65abbf31b0a30290189cdb5059b5f785"><div class="ttname"><a href="garble_2block_8h.html#a65abbf31b0a30290189cdb5059b5f785">block</a></div><div class="ttdeci">__m128i block</div><div class="ttdef"><b>Definition:</b> block.h:8</div></div>
<div class="ttc" id="config_8h_html"><div class="ttname"><a href="config_8h.html">config.h</a></div></div>
<div class="ttc" id="class_hash_html_af482880ad75b67a09d2dcb5e86244d80"><div class="ttname"><a href="class_hash.html#af482880ad75b67a09d2dcb5e86244d80">Hash::Hash</a></div><div class="ttdeci">Hash()</div><div class="ttdef"><b>Definition:</b> hash.h:18</div></div>
<div class="ttc" id="class_hash_html_a4e4b4797dda8678aaed058bae155813e"><div class="ttname"><a href="class_hash.html#a4e4b4797dda8678aaed058bae155813e">Hash::~Hash</a></div><div class="ttdeci">~Hash()</div><div class="ttdef"><b>Definition:</b> hash.h:21</div></div>
<div class="ttc" id="class_hash_html_a86ad3dcd18b0b279d4039e4404a1043c"><div class="ttname"><a href="class_hash.html#a86ad3dcd18b0b279d4039e4404a1043c">Hash::put_block</a></div><div class="ttdeci">void put_block(const block *block, int nblock=1)</div><div class="ttdef"><b>Definition:</b> hash.h:35</div></div>
<div class="ttc" id="class_hash_html_a4bb9da7bf411c5db9af391fc3ec74f81"><div class="ttname"><a href="class_hash.html#a4bb9da7bf411c5db9af391fc3ec74f81">Hash::put</a></div><div class="ttdeci">void put(const void *data, int nbyte)</div><div class="ttdef"><b>Definition:</b> hash.h:23</div></div>
<div class="ttc" id="utils__ec_8h_html"><div class="ttname"><a href="utils__ec_8h.html">utils_ec.h</a></div></div>
<div class="ttc" id="config_8h_html_a248d7b4360cb56aceb6b29be5990ef75"><div class="ttname"><a href="config_8h.html#a248d7b4360cb56aceb6b29be5990ef75">HASH_BUFFER_SIZE</a></div><div class="ttdeci">#define HASH_BUFFER_SIZE</div><div class="ttdef"><b>Definition:</b> config.h:5</div></div>
<div class="ttc" id="class_hash_html"><div class="ttname"><a href="class_hash.html">Hash</a></div><div class="ttdef"><b>Definition:</b> hash.h:12</div></div>
<div class="ttc" id="class_hash_html_a574b6b0ef1b80eb4b15809cc15257a3f"><div class="ttname"><a href="class_hash.html#a574b6b0ef1b80eb4b15809cc15257a3f">Hash::digest</a></div><div class="ttdeci">void digest(char *a)</div><div class="ttdef"><b>Definition:</b> hash.h:38</div></div>
<div class="ttc" id="class_hash_html_afd43dfd1a98f58dd6ea17eb3bba8a6c2"><div class="ttname"><a href="class_hash.html#afd43dfd1a98f58dd6ea17eb3bba8a6c2">Hash::DIGEST_SIZE</a></div><div class="ttdeci">static const int DIGEST_SIZE</div><div class="ttdef"><b>Definition:</b> hash.h:17</div></div>
<div class="ttc" id="class_hash_html_a0b3eafe3161e79b29a1501890bc3bbc0"><div class="ttname"><a href="class_hash.html#a0b3eafe3161e79b29a1501890bc3bbc0">Hash::hash_for_block</a></div><div class="ttdeci">static block hash_for_block(const void *data, int nbyte)</div><div class="ttdef"><b>Definition:</b> hash.h:52</div></div>
<div class="ttc" id="utils_2block_8h_html"><div class="ttname"><a href="utils_2block_8h.html">block.h</a></div></div>
<div class="ttc" id="class_hash_html_a315c7f00ae3d92173fa452006b98501f"><div class="ttname"><a href="class_hash.html#a315c7f00ae3d92173fa452006b98501f">Hash::hash_once</a></div><div class="ttdeci">static void hash_once(void *digest, const void *data, int nbyte)</div><div class="ttdef"><b>Definition:</b> hash.h:49</div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_eec959d81771dc8b1a750b834e1341eb.html">emp-tool</a></li><li class="navelem"><a class="el" href="dir_363cbed62d1567d90b8cbb6bda69428f.html">utils</a></li><li class="navelem"><a class="el" href="hash_8h.html">hash.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.12 </li>
  </ul>
</div>
</body>
</html>
